<template>
	<view class="">
		<view style="width: 100%; height: 100vh">
			<j-cell-group v-if="list.length > 0">
				<j-cell v-for="(item,index) in list" :key="index" :more="false" @jftap="radioChange(index)">
					<view slot="title" class="title">
						{{type == 1?item.name:item}}
					</view>
					<view slot="tips" style="padding-right:10px;" v-if="index === id">
						<view class="gou"></view>
					</view>
				</j-cell>
			</j-cell-group>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				token: '',
				type: 1,
				list: [],
				id: -1,
				lanQuery: {},
				sleepQuery: {},
				time: 0
			}
		},
		onLoad(opt) {
			this.token = opt.token
			this.type = opt.type
			let title = '设备语言'
			if (this.type == 1) {
				this.getDeviceLanguage()
			} else if (this.type == 2) {
				title = '设备自动休眠时间'
				this.list = ['15秒', '30秒', '永不休眠']
				this.getSleepTime()
			}
			uni.setNavigationBarTitle({
				title: title
			})

		},
		onBackPress() {
			this.onSubmit()
		},
		methods: {
			// 选择
			radioChange(index) {
				this.id = index;
				//语言
				if (this.type == 1) {
					this.lanQuery['General.Location'].Language = this.list[index]
					this.setConfig(this.lanQuery)
				} else {
					// 睡眠
					let data = [15, 30, -2]
					this.sleepQuery['System.ManageShutDown'].ShutDownMode = data[index]
					this.setConfig(this.sleepQuery)
				}
			},
			// 获取语言列表
			getability(data) {
				let token = this.token;
				getApp().globalData.api.getability(data, token, (result) => {
					console.log('获取参数：', result);
					if (result.Name == 'MultiLanguage') {
						// this.list = result.MultiLanguage
						this.list = result.MultiLanguage.map(item => {
							let obj = {
								name: item == "SimpChinese" ? '中文' : '英文',
								value: item
							}
							return obj
						})
						console.log(this.list)
						this.list.forEach((item, index) => {
							if (item.value == this.language) {
								this.id = index
							}
						})
					}
				})
			},

			//获取当前设备语言
			getDeviceLanguage() {
				let data = {
					Name: 'General.Location'
				}
				this.getConfig(data)
			},
			//获取当前睡眠时间
			getSleepTime() {
				let data = {
					Name: 'System.ManageShutDown'
				}
				this.getConfig(data)
			},
			// 获取配置
			getConfig(data) {
				let token = this.token;
				getApp().globalData.api.getconfig(data, token, (result) => {
					console.log('获取参数：', result);
					if (result.Name == 'General.Location') {
						this.lanQuery = result
						this.language = result['General.Location'].Language
						let data = {
							Name: 'MultiLanguage'
						}
						this.getability(data)
					}
					if (result.Name == 'System.ManageShutDown') {
						this.sleepQuery = result
						let time = result['System.ManageShutDown'].ShutDownMode
						if (time == -2) {
							this.id = 2
						}
						this.list.forEach((item, index) => {
							if (item.includes(time)) {
								this.id = index
							}
						})
					}
				})
			},
			//设置参数
			setConfig(data) {
				let token = this.token;
				getApp().globalData.api.setconfig(data, token, (result) => {
					console.log('设置参数：', result);
				})
			},
			onSubmit() {
				let data = {
					Name: 'Camera'
				}
				let token = this.token;
				getApp().globalData.api.getconfig(data, token, (result) => {
					console.log('参数：', result);
				})
			},


		}
	}
</script>

<style>
	.title {
		width: 70vw;
		word-wrap: break-word;
		word-break: break-all;
	}

	.gou {
		width: 18px;
		height: 9px;
		border: 2px solid #1AC0BA;
		border-bottom: none;
		border-left: none;
		transform: rotate(135deg);
	}
</style>
